
/**
 * @author Max Granz, Daniel Weber
 */
import javax.microedition.lcdui.game.LayerManager;
import javax.microedition.lcdui.game.TiledLayer;

public class level extends LayerManager {

    //Array der Karten - werden in InputStreams.java geladen
    public static int[][] mapsLeicht;
    public static int[][] mapsMittel;
    public static int[][] mapsSchwer;
    //Anzahl der Vertikalen Blöcke
    public static final int ZEILEN = 12;
    private static int[] mapArray;
    public static TiledLayer oldMapLayer,//Das alte Level, das noch gerendert wird
            newMapLayer;//das Level, auf dem sich der GMan befindet
    public static int position,//Kameraposition
            stufe = 0,//Spielstufe
            blockHeight = 20,
            blockWidth = 20;

    public level() {
        stufe = 0;
        Levelaufbau();
        for (int i = 0; i < 4; i++) {
            append(Game.gMan[i]);
        }
        if (Game.iSpielerZahl == 1) {
            Game.gMan[0].setPosition(blockWidth, newMapLayer.getY() + blockHeight * 19);
        } else {
            if (Game.gMan[0] != null) {
                Game.gMan[0].setPosition(blockWidth, newMapLayer.getY() + blockHeight * 19);
            }
            if (Game.gMan[1] != null) {
                Game.gMan[1].setPosition(blockWidth * 8, newMapLayer.getY() + blockHeight * 19);
                Game.gMan[1].switchGravity();
            }
            if (Game.gMan[2] != null) {
                Game.gMan[2].setPosition(blockWidth * 3, newMapLayer.getY() + blockHeight * 19);
            }
            if (Game.gMan[3] != null) {
                Game.gMan[3].setPosition(blockWidth * 6, newMapLayer.getY() + blockHeight * 19);
                Game.gMan[3].switchGravity();
            }

        }
        position = system.getXLayer(Game.gMan[0]);
    }
/**
 * Aktualisiert das Level, lädt ein neues Level
 */
    public final void Levelaufbau() {
        if (newMapLayer != null) {
            remove(newMapLayer);
        }
        if (oldMapLayer != null) {
            remove(oldMapLayer);
        }

        mapArray = Mapauswahl();
        stufe++;

        //neues MapLayer in das alte schieben
        if (newMapLayer != null) {
            oldMapLayer = newMapLayer;
        }

        //neues MapLayer erstellen
        newMapLayer = new TiledLayer(ZEILEN, mapArray.length / ZEILEN, system.img[system.mapPNG], blockWidth, blockHeight);
        for (int i = 0; i < mapArray.length; i++) {
            int row = i % ZEILEN;
            int column = i / ZEILEN;
            newMapLayer.setCell(row, column, mapArray[i]);
        }

        //Position des neuen MapLayers setzen
        if (oldMapLayer != null) {
            newMapLayer.setPosition(0, oldMapLayer.getY() + oldMapLayer.getHeight());
        } else {
            oldMapLayer = newMapLayer;
        }

        append(newMapLayer);
        append(oldMapLayer);
    }

    /**
     * Liefert ein zur Spielstufe passendes Level
     * @return int[]
     */
    private int[] Mapauswahl() {
        if (stufe == 0) {
            return initMap;
        } else if (stufe <= 4) {
            int mapIndex = system.getRnd(mapsLeicht.length - 1);
            return mapsLeicht[mapIndex];
        } else if (stufe > 4 && stufe <= 7) {
            int mapIndex = system.getRnd(mapsMittel.length - 1);
            return mapsMittel[mapIndex];
        } else {
            int mapIndex = system.getRnd(mapsSchwer.length - 1);
            return mapsSchwer[mapIndex];
        }

    }
    //Eine simples Anfangslevel zum Spawnen
    public static final int[] initMap = {
        4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4,
        4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4,
        4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4,
        4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4,
        4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4,
        4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4,
        4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4,
        4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4,
        4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4,
        4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4,
        4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4,
        4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4,
        4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4,
        4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4,
        4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4,
        4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4,
        4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4,
        4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4,
        4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4,
        4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4,
        4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4,
        4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4,
        4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4,
        4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4,};
}
